Subject: Re: RKM description of WaitPort()
From: [email-masked] (Peter Cherna)
Date: 5 Jan 93 15:50:15 GMT
Newsgroups: comp.sys.amiga.programmer

In article <[email-masked]> [email-masked] (Gregory R Block) writes:
>In article <[email-masked]> [email-masked] (Quentin Barnes) writes:
>>It seems to me to OS is waking up the task before it has attached the
>>message to the specified port

>No, it's just possible to send a signal to a port without sending a
>message.

In general (99.999%?) one doesn't signal a port without sending a
message.  That's not the issue at all.  I'll show two quite possible
scenarios for the order of message arrival and application processing
of messages, and you'll see quite easily how the signal might be
set even though there are no waiting messages.

Because messages queue but signals don't, there can be multiple
messages for one signal (in this case two).  Thus, an application
must remove ALL messages from the message port when it wakes up,
which is why you often see:

	while ( msg = GetMsg( port ) )
	{
	    ...
	}

Scenario A:

 - Message 1 arrives and is queued, and the signal is set.

 - Message 2 arrives and is queued, and signal is set (which has
   no effect, since the signal was already set).

 - Your application wakes up, clearing the signal.

 - The application GetMsg()s Message 1 and processes it.

 - The application GetMsg()s Message 2 and processes it.

 - The application finds no further messages, so Wait()s for the next
   signal.

OK, now consider a slightly different order of arrival:

Scenario B:

 - Message 1 arrives and is queued, and the signal is set.

 - Your application wakes up, clearing the signal.

 - The application  GetMsg()s Message 1 and starts to process it.

 - Message 2 arrives and is queued, and the signal is set.

 - Your application finishes with Message 1, and like all well-written
   applications, you loop back to see if there are any more messages
   queued, and you find Message 2, so you GetMsg() that and process
   it.  NOTE that you didn't WaitPort() a second time, so the signal
   is not cleared.

 - You're done with Message 2, and find no further messages on your
   port, so you WaitPort().

 - You wake up immediately because there was a left-over signal, as
   described above.  So you check for messages, and there are none.

To summarize:  because messages queue and signals do not, you have to
exhaust the message port for every signal received.  Because you do
that, you might end up with a set signal whose messages you've already
processed.

     Peter
--
Peter Cherna, User Interface Development Group, Commodore-Amiga, Inc.
{uunet|rutgers}!cbmvax!peter    [email-masked]
My opinions do not necessarily represent the opinions of my employer.
"Opinions enlarged to show detail"